﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace FractionServiceLibary
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Fraction1" in both code and config file together.
    public class FractionArihmeticService : IFractionArihmeticService
    {
        public Fraction Add(Fraction fra1, Fraction fra2)
        {
            fra1.Numerator = fra1.Numerator * fra2.Denominator + fra1.Denominator * fra2.Numerator;
            fra1.Denominator = fra1.Denominator * fra2.Denominator;

            return Shortcut(fra1);
        }

        public Fraction Subtract(Fraction fra1, Fraction fra2)
        {
            fra1.Numerator = fra1.Numerator * fra2.Denominator - fra1.Denominator * fra2.Numerator;
            fra1.Denominator = fra1.Denominator * fra2.Denominator;

            return Shortcut(fra1);
        }

        public Fraction Multiply(Fraction fra1, Fraction fra2)
        {
            fra1.Numerator = fra1.Numerator * fra2.Numerator;
            fra1.Denominator = fra1.Denominator * fra2.Denominator;

            return Shortcut(fra1);
        }

        public Fraction Division(Fraction fra1, Fraction fra2)
        {
            fra1.Numerator = fra1.Numerator * fra2.Denominator;
            fra1.Denominator = fra1.Denominator * fra2.Numerator;

            return Shortcut(fra1);
        }

        public int GDivisor(int a, int b)
        {
            a = Math.Abs(a);
            b = Math.Abs(b);

            if (a == 0 || b == 0)
            {
                return (a + b);
            }
            while (a != b)
            {
                if (a > b)
                {
                    a = (a - b);
                }
                else
                {
                    b = (b - a);
                }
            }

            return a;
        }

        public Fraction Shortcut(Fraction fra)
        {
            int temp = GDivisor(fra.Numerator, fra.Denominator);

            fra.Numerator = fra.Numerator / temp;
            fra.Denominator = fra.Denominator / temp;
            return fra;
        }
    }
}
